package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import net.sf.json.JSONSerializer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Date; 
import java.util.List; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXADMChiNhanh;
import com.lb.jwitter.service.TblQLDTTXADMChiNhanhService;
import com.lb.jwitter.service.TblQLDTTXADMGiayTo;
import com.lb.jwitter.service.TblQLDTTXADMKhoa;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHoc;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHocService;
import com.lb.jwitter.service.TblQLDTTXADMKhoaService;
import com.lb.jwitter.service.TblQLDTTXADMNganh;
import com.lb.jwitter.service.TblQLDTTXADMNganhService;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQLDTTXALopService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAThongBaoTuyenSinh;
import com.lb.jwitter.service.TblQLDTTXAThongBaoTuyenSinhService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXABienNhanHoSo;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoService;
import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyenService;

import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayTo;

import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayToService;

import com.lb.jwitter.service.TblQLDTTXADMGiayToService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXABienNhanHoSoReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private TblQLDTTXABienNhanHoSo tblQLDTTXABienNhanHoSo; 
    public void setTblQLDTTXABienNhanHoSo (TblQLDTTXABienNhanHoSo tblQLDTTXABienNhanHoSo) {
        this.tblQLDTTXABienNhanHoSo = tblQLDTTXABienNhanHoSo;
 }

    private Collection<TblQLDTTXABienNhanHoSo> tblQLDTTXABienNhanHoSos; 
    public Collection<TblQLDTTXABienNhanHoSo>  getTblQLDTTXABienNhanHoSos() {
        return this.tblQLDTTXABienNhanHoSos ; 
      } 

    private TblQLDTTXABienNhanHoSoService tblQLDTTXABienNhanHoSoService; 

    private TblCanBoService tblCanBoService; 

    private TblQLDTTXAHoSoXetTuyenService tblQLDTTXAHoSoXetTuyenService; 

    private String tungayNhan; 
    public void setTungayNhan(String tungayNhan) {
       this.tungayNhan = tungayNhan;
   }

    public String getTungayNhan() {
       return tungayNhan;
   }

    private String denngayNhan; 
    public void setDenngayNhan(String denngayNhan) {
       this.denngayNhan = denngayNhan;
   }

    public String getDenngayNhan() {
       return denngayNhan;
   }

    private String soBienLai; 
    public void setSoBienLai(String soBienLai) {
       this.soBienLai = soBienLai;
   }

    public String getSoBienLai() {
       return soBienLai;
   }

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
  }

    public int getCanBoId() {
       return canBoId;
  }

    private Collection<TblCanBo> tblCanBos; 
    public Collection<TblCanBo>  getTblCanBos() {
        return this.tblCanBos ; 
      } 

    private int hoSoId; 
    public void setHoSoId(int hoSoId) {
       this.hoSoId = hoSoId;
  }
    private Long thongBaoId;
    
    private List<TblQLDTTXAThongBaoTuyenSinh> tblQLDTTXAThongBaoTuyenSinhs;
    private TblQLDTTXAThongBaoTuyenSinhService tblQLDTTXAThongBaoTuyenSinhService;

    public int getHoSoId() {
       return hoSoId;
  }
    
    private String report;

    private Collection<TblQLDTTXAHoSoXetTuyen> tblQLDTTXAHoSoXetTuyens; 
    public Collection<TblQLDTTXAHoSoXetTuyen>  getTblQLDTTXAHoSoXetTuyens() {
        return this.tblQLDTTXAHoSoXetTuyens ; 
      } 

    private TblQLDTTXABienNhanHoSoGiayToService tblQLDTTXABienNhanHoSoGiayToService; 

    private Collection<TblQLDTTXABienNhanHoSoGiayTo> tblQLDTTXABienNhanHoSoGiayTos; 
    public Collection<TblQLDTTXABienNhanHoSoGiayTo>  getTblQLDTTXABienNhanHoSoGiayTos() {
        return this.tblQLDTTXABienNhanHoSoGiayTos ; 
      } 

    private Collection<TblQLDTTXADMGiayTo> tblQLDTTXADMGiayTos; 
    public Collection<TblQLDTTXADMGiayTo>  getTblQLDTTXADMGiayTos() {
        return this.tblQLDTTXADMGiayTos ; 
      } 

    private TblQLDTTXADMGiayToService tblQLDTTXADMGiayToService; 


    private TblQLDTTXADMKhoaService tblQLDTTXADMKhoaService;
    private TblQLDTTXADMNganhService tblQLDTTXADMNganhService;
    private TblQLDTTXADMChiNhanhService tblQLDTTXADMChiNhanhService;
    
    
    private List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas;
    private List<TblQLDTTXADMNganh> tblQLDTTXADMNganhs;
    private List<TblQLDTTXADMChiNhanh> tblQLDTTXADMChiNhanhs;
    
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider;
	 
	 
	 
public String form() throws Exception 
{ 
	 try{ 	
		     Map session = ActionContext.getContext().getSession();
		     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
		     if( taiKhoanDangNhap == null) return "noprivilege";
		    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
		    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
		    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "xem")){ 
		        return "noprivilege"; 
		    } 
	 
	        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 
	        tblQLDTTXAThongBaoTuyenSinhService = (TblQLDTTXAThongBaoTuyenSinhService) context.getBean("tbl_QLDTTXA_ThongBaoTuyenSinhClient"); 
	        
	        tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient");
	        tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient");
	        tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient");
	       
	        msg = new Message(); 
	        
	        
	        tblQLDTTXADMChiNhanhs = tblQLDTTXADMChiNhanhService.findAlltblQLDTTXADMChiNhanh(0, 0);
	        tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findAlltblQLDTTXADMKhoa(0, 0);
	        tblQLDTTXADMNganhs = tblQLDTTXADMNganhService.findAlltblQLDTTXADMNganh(0, 0);
	        
	        List<ConditionReport> cons = new ArrayList<ConditionReport>();
	        
	        { 
	            ConditionReport con = new ConditionReport(); 
	            con.setCol("ten");
	            con.setValue1("asc");
	            con.setOperator("order");
	            cons.add(con);	
	        } 
	        tblQLDTTXADMChiNhanhs = tblQLDTTXADMChiNhanhService.findtblQLDTTXADMChiNhanhByCondition(cons, 0, 0);
	        tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findtblQLDTTXADMKhoaByCondition(cons, 0, 0);
	        tblQLDTTXADMNganhs = tblQLDTTXADMNganhService.findtblQLDTTXADMNganhByCondition(cons, 0, 0);
	        
	        cons.clear();
	        { 
	            ConditionReport con = new ConditionReport(); 
	            con.setCol("thoiGianBatDau");
	            con.setValue1("desc");
	            con.setOperator("order");
	            cons.add(con);	
	        } 
	        tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhService.findtblQLDTTXAThongBaoTuyenSinhByCondition(cons, 30, 1);
	        
	        cons.clear();
	        { 
	            ConditionReport con = new ConditionReport(); 
	            con.setCol("ten");
	            con.setValue1("asc");
	            con.setOperator("order");
	            cons.add(con);	
	        } 
	        tblCanBos = tblCanBoService.findtblCanBoByCondition(cons, 0,0); 
	 
	        return Action.SUCCESS; 
	 }catch(Exception e){ 
	         System.out.println("Loi Report Form tblQLDTTXABienNhanHoSo Action"); 
	         e.printStackTrace(); 
	         return Action.ERROR; 
	 } 
}
	 
private Long khoaId;
private Long nganhId;
private Long chiNhanhId;

 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "xem")){ 
        return "noprivilege"; 
    } 
 
        tblQLDTTXABienNhanHoSoService = (TblQLDTTXABienNhanHoSoService) context.getBean("tbl_QLDTTXA_BienNhanHoSoClient"); 
        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 


        tblQLDTTXAHoSoXetTuyenService = (TblQLDTTXAHoSoXetTuyenService) context.getBean("tbl_QLDTTXA_HoSoXetTuyenClient"); 
        tblQLDTTXAThongBaoTuyenSinhService = (TblQLDTTXAThongBaoTuyenSinhService) context.getBean("tbl_QLDTTXA_ThongBaoTuyenSinhClient"); 

        tblQLDTTXABienNhanHoSoGiayToService = (TblQLDTTXABienNhanHoSoGiayToService) context.getBean("tbl_QLDTTXA_BienNhanHoSo_GiayToClient"); 

        tblQLDTTXADMGiayToService = (TblQLDTTXADMGiayToService) context.getBean("tbl_QLDTTXA_DM_GiayToClient"); 

        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>();

        cons.clear();
    if( tungayNhan != null && !tungayNhan.equalsIgnoreCase("") ){ 
        ConditionReport con = new ConditionReport(); 
        con.setType("date"); 
         con.setCol("ngayNhan");
        con.setValue1(tungayNhan);
        if (denngayNhan != null && !denngayNhan.equalsIgnoreCase("") ){ 
            con.setValue2(denngayNhan); 
            con.setOperator("between"); 
        } 
        else{ 
            con.setOperator("ge"); 
        } 
        cons.add(con);	
    } 
        else if (denngayNhan != null && !denngayNhan.equalsIgnoreCase("") ){ 
        ConditionReport con = new ConditionReport(); 
        con.setType("date"); 
         con.setCol("ngayNhan");
        con.setValue1(denngayNhan);
        con.setOperator("le");
        cons.add(con);	
    } 

    if(canBoId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("canBoId.id");
        con.setValue1(new Long(canBoId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    
    if(thongBaoId != null && !thongBaoId.equals(new Long(0)) ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hoSoId.thongBaoId.id");
        con.setValue1(new Long(thongBaoId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(khoaId != null && !khoaId.equals(new Long(0))){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hoSoId.khoaId.id");
        con.setValue1(khoaId);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(nganhId != null && !nganhId.equals(new Long(0))){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hoSoId.nganhId.id");
        con.setValue1(nganhId);
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(chiNhanhId != null && !chiNhanhId.equals(new Long(0))){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hoSoId.chiNhanhId.id");
        con.setValue1(chiNhanhId);
        con.setOperator("eq");
        cons.add(con);	
    } 
    
    { 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hoSoId.ten");
        con.setValue1("asc");
        con.setOperator("order");
        cons.add(con);	
    }
    
    { 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hoSoId.hoTen");
        con.setValue1("asc");
        con.setOperator("order");
        cons.add(con);	
    } 


  tblQLDTTXABienNhanHoSos = tblQLDTTXABienNhanHoSoService.findtblQLDTTXABienNhanHoSoByCondition(cons,0,0); 
  tblQLDTTXADMGiayTos = tblQLDTTXADMGiayToService.findAlltblQLDTTXADMGiayTo(0,0); 
  

  //sinh report
  int colNo = 0;
  
  report ="";
  int stt = 0;
  for(TblQLDTTXABienNhanHoSo bienNhanHoSo : tblQLDTTXABienNhanHoSos){ 
	   cons.clear();
	   if(bienNhanHoSo != null){ 
	       ConditionReport con = new ConditionReport(); 
	       con.setCol("bienNhanId.id");
	       con.setValue1(bienNhanHoSo.getId());
	       con.setOperator("eq");
	       cons.add(con);	
	   } 

	   List<TblQLDTTXABienNhanHoSoGiayTo> tblQLDTTXABienNhanHoSoGiayTos = tblQLDTTXABienNhanHoSoGiayToService.findtblQLDTTXABienNhanHoSoGiayToByCondition( cons,0,0);
      DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
      
      stt++;
      report += "<tr class='tableitem' > "; 
      report += "<TD>" +stt+ "</TD>"; 
      report += "<td>" +bienNhanHoSo.getHoSoId().getHoTen()+ "</td> ";
      report += "<td>" +bienNhanHoSo.getHoSoId().getTen()+ "</td> ";
      report += "<td>" +bienNhanHoSo.getSoBienLai()+ "</td>";
      try{
    	  report += "<td>" +bienNhanHoSo.getNgayNhan().getDay()+"/"+bienNhanHoSo.getNgayNhan().getMonth()+"/"+bienNhanHoSo.getNgayNhan().getYear()+ "</td>";
      }catch(Exception e){
    	  report += "<td></td>"; 
            
      }
      try{

          report += "<td>" +bienNhanHoSo.getCanBoId().getTen()+ "</td>";  
      }catch(Exception e){
    	  report += "<td></td>"; 
      }


	   for(TblQLDTTXADMGiayTo giayTo : tblQLDTTXADMGiayTos){
		   Boolean b = false;
		   for(TblQLDTTXABienNhanHoSoGiayTo bienNhanHoSoGiayTo : tblQLDTTXABienNhanHoSoGiayTos){
			   if(bienNhanHoSoGiayTo.getGiayToId().getId().equals(giayTo.getId())){
			       b = true;
			       break;
			   }
		   }//end-for BienNhanGiay To
		   if(b)
			   report += "<TD>*</TD>";
		   else
			   report += "<TD></TD>"; 
	   }//end for GiayTo
	   report +=  "</tr> ";
  }
  
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXABienNhanHoSo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getReport() {
	return report;
}

public void setReport(String report) {
	this.report = report;
}

public Long getThongBaoId() {
	return thongBaoId;
}

public void setThongBaoId(Long thongBaoId) {
	this.thongBaoId = thongBaoId;
}

public List<TblQLDTTXAThongBaoTuyenSinh> getTblQLDTTXAThongBaoTuyenSinhs() {
	return tblQLDTTXAThongBaoTuyenSinhs;
}

public void setTblQLDTTXAThongBaoTuyenSinhs(
		List<TblQLDTTXAThongBaoTuyenSinh> tblQLDTTXAThongBaoTuyenSinhs) {
	this.tblQLDTTXAThongBaoTuyenSinhs = tblQLDTTXAThongBaoTuyenSinhs;
}

public Long getKhoaId() {
	return khoaId;
}

public void setKhoaId(Long khoaId) {
	this.khoaId = khoaId;
}

public Long getNganhId() {
	return nganhId;
}

public void setNganhId(Long nganhId) {
	this.nganhId = nganhId;
}

public Long getChiNhanhId() {
	return chiNhanhId;
}

public void setChiNhanhId(Long chiNhanhId) {
	this.chiNhanhId = chiNhanhId;
}

public List<TblQLDTTXADMKhoa> getTblQLDTTXADMKhoas() {
	return tblQLDTTXADMKhoas;
}

public List<TblQLDTTXADMNganh> getTblQLDTTXADMNganhs() {
	return tblQLDTTXADMNganhs;
}

public List<TblQLDTTXADMChiNhanh> getTblQLDTTXADMChiNhanhs() {
	return tblQLDTTXADMChiNhanhs;
} 
 } 
 
